from spass.api.api_db import (add_user,
                              add_user_to_team,
                              add_team_to_teamset,
                              new_template,
                              create_assignment)
from spass.api.api_warehouse import (wnew_assignment_version,
                                     wadd_assignment_version_file)
from spass.core.error import error
import datetime
from optparse import OptionParser

def callback_add_user(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    add_user(parser.values.user[0], parser.values.user[1])

def callback_add_user_to_team(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    add_user_to_team(int(parser.values.team[0]), parser.values.team[1], parser.values.team[2])

def callback_add_team_to_teamset(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    add_team_to_teamset(parser.values.teamset[0], parser.values.teamset[1])

def callback_new_template(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    new_template(parser.values.template[0], parser.values.template[1])

def callback_create_assignment(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    date_parse = parser.values.assignment[1].split('-')
    
    if not len(date_parse) == 3:
        error('date.assert', [])
        return 1
    elif date_parse[0] == "" or date_parse[1] == "" or date_parse[2] == "":
        error('date.assert', [])
        return 1
    
    year_now = datetime.date.today()
    year_now = year_now.year
    
    if int(date_parse[0]) < year_now or int(date_parse[0]) > year_now+4:
        error('year.assert', [str(year_now), str(year_now+4)])
        return 1
    if int(date_parse[1]) < 1 or int(date_parse[1]) > 12:
        error('month.assert', [])
        return 1
    if int(date_parse[2]) < 1 or int(date_parse[2]) > 31:
        error('day.assert', [])
        return 1
    
    thedate = datetime.date(int(date_parse[0]), int(date_parse[1]), int(date_parse[2]))
    create_assignment(int(parser.values.assignment[0]), thedate, int(parser.values.assignment[2]), int(parser.values.assignment[3]))

def callback_new_assignment_version(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    wnew_assignment_version(parser.values.version[0], parser.values.version[1])
        
def callback_add_assignment_version_file(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
    wadd_assignment_version_file(parser.values.file[0], parser.values.file[1], parser.values.file[2], parser.values.file[3])

def add(argv):
    if len(argv) < 1:
        error('args.need', ['add', ''])
	return 1
    
    parser = OptionParser()
    parser.set_usage("""spass add <options...>""")
    parser.add_option("--user", action="callback", callback=callback_add_user, type="string", nargs=2, dest="user",
                      help="Adds an user to the database. ARG : <first name> <second name>")
    parser.add_option("--team", action="callback", callback=callback_add_user_to_team, type="string", nargs=3, dest="team",
                      help="Adds an user to a team. ARG : <team ID> <first name> <second name>")   
    parser.add_option("--teamset", action="callback", callback=callback_add_team_to_teamset, type="int", nargs=2, dest="teamset", 
                      help="Adds an team to a teamset. ARG : <teamset ID> <team ID>")    
    parser.add_option("--template", action="callback", callback=callback_new_template, type="string", nargs=2, dest="template",
                      help="Creates an template. ARG : <title> <test path>")                      
    parser.add_option("--assignment", action="callback", callback=callback_create_assignment, type="string", nargs=4, dest="assignment", 
                      help="Creates an assignment. ARG : <template ID> <date YYYY-MM-DD> <teamset ID> <supervisor ID>")
    parser.add_option("--assignment-ver", action="callback", callback=callback_new_assignment_version, type="string", nargs=2, dest="version",
                      help="Creates an assignment version. ARG : <assignment ID> <team ID>")
    parser.add_option("--assignment-ver-file", action="callback", callback=callback_add_assignment_version_file, type="string", nargs=4, dest="file",
                      help="Adds a file to an assignment version. ARG : <assignment ID> <team ID> <version ID> <file path>")

    (options, args) = parser.parse_args(argv)
    return 0
